Crate reopen

source ·
Expand description

A tiny Read/Write wrapper that can reopen the underlying IO object.

The main motivation is integration of logging with logrotate. Usually, when logrotate wants to rotate log files, it moves the current log file to a new place and creates a new empty file. However, for the new messages to appear in the new file, a running program needs to close and reopen the file. This is most often signalled by SIGHUP.

§Examples

This allows reopening the IO object used inside the logging drain at runtime.

extern crate libc;
#[macro_use]
extern crate log;
extern crate reopen;
extern crate simple_logging;

use std::fs::{File, OpenOptions};
use std::io::Error;

use reopen::Reopen;

fn open() -> Result<File, Error> {
    OpenOptions::new()
        .create(true)
        .write(true)
        .append(true)
        .open("/dev/null")
}

fn main() -> Result<(), Error> {
    let file = Reopen::new(Box::new(&open))?;
    file.handle().register_signal(libc::SIGHUP).unwrap();
    simple_logging::log_to(file, log::LevelFilter::Debug);
    info!("Hey, it's logging");
    Ok(())
}

Note that this solution is a bit hacky and probably solves only the most common use case.

If you find another use case for it, I’d like to hear about it.

Structs§

  • A handle to signal a companion Reopen object to do a reopen on its next operation.
  • A Read/Write proxy that can reopen the underlying object.